package kitread.util;

import de.l3s.boilerpipe.BoilerpipeExtractor;
import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.CommonExtractors;

public class TextExtractor {

	private BoilerpipeExtractor extractor = CommonExtractors.ARTICLE_EXTRACTOR;
	private static TextExtractor instance = new TextExtractor();
	
	public static TextExtractor instance() {
		return instance;
	}
	
	public String getText(String htmlText) {
		try {
			return extractor.getText(htmlText).trim();
		} catch (BoilerpipeProcessingException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public String trimString(String string, int length) {
		if (string == null || string.trim().isEmpty()) {
			return string;
		}
		return string.replaceAll("(?<=.{"+length+"})\\b.*", "...");
	}
}
